package com.wonders.frame.chron.service.catalog;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.wonders.frame.chron.dao.catalog.CatalogDao;
import com.wonders.frame.chron.model.bo.Catalog;
import com.wonders.frame.console.model.vo.Ticket;
import com.wonders.frame.core.model.IDefaultModel;
import com.wonders.frame.core.model.bo.Relation;
import com.wonders.frame.core.model.vo.RelatedNode;
import com.wonders.frame.core.model.vo.ReturnObj;
import com.wonders.frame.core.model.vo.ReturnObjInfo.ErrorType;
import com.wonders.frame.core.service.BasicCrudService;
import com.wonders.frame.core.service.RelationService;

@Service
public class CatalogService {

	@Resource
	private CatalogDao catalogDao;

	@Resource
	private RelationService relationService;

	@Resource
	private BasicCrudService basicCrudService;

	/**
	 * 添加目录
	 * 
	 * @param c
	 * @param arg0
	 */
	@Transactional
	public void save(Catalog arg0) {
		catalogDao.save(arg0);
	}
	/**
	 * 获取子级目录
	 * @param id
	 * @param sortField
	 * @param sortAsc
	 * @param neededObj
	 * @param lv
	 * @param ticket
	 * @return
	 */
	public ReturnObj<List<RelatedNode<IDefaultModel, Integer>>> getCatalogChild(
			Integer id, String sortField,String sortAsc,String neededObj, int lv, 
			 String ticket) {

		Ticket t = new Ticket(ticket);
		if (t.getRuleTypeId() == null) {
			return new ReturnObj<List<RelatedNode<IDefaultModel, Integer>>>(
					"invalid ticket");
		}
		// 获取关联关系
		List<Relation> relations = relationService.findAllChildRelation(
				t.getRuleTypeId(), "catalog", id, lv);
		// 往关联关系上绑定对象
		List<RelatedNode<IDefaultModel, Integer>> relatedNodes = basicCrudService
				.bindChildObjInfo(relations);
		// 资源树排序
		relatedNodes = basicCrudService.objSort(relatedNodes, sortField,
				sortAsc);
		// 资源树过滤
		if (!neededObj.equals("")) {
			relatedNodes = basicCrudService.objFilter(relatedNodes, neededObj);
		}

		if (relatedNodes == null || relatedNodes.size() == 0) {
			return new ReturnObj<List<RelatedNode<IDefaultModel, Integer>>>(
					ErrorType.NULL);
		}
		return new ReturnObj<List<RelatedNode<IDefaultModel, Integer>>>(
				relatedNodes);
	}

	/**
	 * 获取目录
	 * @param sortField
	 * @param sortAsc
	 * @param neededObj
	 * @param lv
	 * @param ticket
	 * @param resourceType
	 * @param type
	 * @return
	 */
	public List<Map<String, Object>> getCatalogs(String sortField,
			String sortAsc, String neededObj, int lv, String ticket,
			String resourceType,String type) {
		ReturnObj<List<RelatedNode<IDefaultModel, Integer>>> childNode =getCatalogChild(1, sortField, sortAsc, neededObj, lv, ticket);
		List<RelatedNode<IDefaultModel, Integer>> data = childNode.getData();
		// 创建集合
		List<Map<String, Object>> catalogList = new ArrayList<Map<String, Object>>();
		for (RelatedNode<IDefaultModel, Integer> n : data) {
			Catalog c = (Catalog) n.getNode();
			if (c.getType().equals(type) &&c.getResourceType().equals(resourceType)) {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("nobj", n.getNobj());
				map.put("pobj", n.getPobj());
				map.put("nid", n.getNid());
				map.put("pid", n.getPid());
				map.put("node", c);
				catalogList.add(map);
			}
		}
		return catalogList;
	}
}
